﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ArgusLib.Controls
{
	public partial class MessageBox : BaseForm
	{
		MessageBox()
		{
			InitializeComponent();
			this.AutoSize = true;
			this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
			this.lText.Location = new Point(this.lText.Margin.Left, this.lText.Margin.Top);
			this.flpButtons.Location = new Point(
				this.ClientSize.Width - this.flpButtons.Width - this.flpButtons.Margin.Right,
				this.ClientSize.Height - this.flpButtons.Height - this.flpButtons.Margin.Bottom);
		}

		public static DialogResult Show(string text, string caption, Buttons buttons)
		{
			MessageBox box = new MessageBox();
			box.Message = text;
			box.Caption = caption;
			box.SetButtons(buttons);
			box.ShowDialog();
			return box.DialogResult;
		}

		public static DialogResult Show(string text, string caption)
		{
			return MessageBox.Show(text, caption, Buttons.OK);
		}

		public static DialogResult Show(string text)
		{
			return MessageBox.Show(text, string.Empty);
		}

		string Message
		{
			get { return this.lText.Text; }
			set
			{
				this.lText.Text = value;
			}
		}

		string Caption
		{
			get { return this.Text; }
			set { this.Text = value; }
		}

		void SetButtons(Buttons buttons)
		{
			this.ControlBox = buttons == Buttons.None;
			this.bAbort.Visible = buttons.HasFlag(Buttons.Abort);
			this.bCancel.Visible = buttons.HasFlag(Buttons.Cancel);
			this.bIgnore.Visible = buttons.HasFlag(Buttons.Ignore);
			this.bNo.Visible = buttons.HasFlag(Buttons.No);
			this.bOK.Visible = buttons.HasFlag(Buttons.OK);
			this.bRetry.Visible = buttons.HasFlag(Buttons.Retry);
			this.bYes.Visible = buttons.HasFlag(Buttons.Yes);
		}

		public override Size GetPreferredSize(Size proposedSize)
		{
			Size clientSize = new Size(
				Math.Max(this.lText.Width + this.lText.Margin.Horizontal, this.flpButtons.Width + this.flpButtons.Margin.Horizontal),
				this.lText.Bottom + Math.Max(this.lText.Margin.Bottom, this.flpButtons.Margin.Top) + this.flpButtons.Height + this.flpButtons.Margin.Bottom);
			Size RetVal = this.SizeFromClientSize(clientSize);
			return RetVal;
		}

		private void bOK_Click(object sender, EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.OK;
		}

		private void bYes_Click(object sender, EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.Yes;
		}

		private void bNo_Click(object sender, EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.No;
		}

		private void bCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		}

		private void bAbort_Click(object sender, EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.Abort;
		}

		private void bRetry_Click(object sender, EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.Retry;
		}

		private void bIgnore_Click(object sender, EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.Ignore;
		}

		[Flags]
		public enum Buttons
		{
			None = 0,
			OK = 1,
			Yes = 1 << 1,
			No = 1 << 2,
			Cancel = 1 << 3,
			Abort = 1 << 4,
			Retry = 1 << 5,
			Ignore = 1 << 6
		}
	}
}
